package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.datasource.Page;
import com.pesking.core.exception.SystemException;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.StringUtils;

public class PagationParameterSqlHandler extends SqlParameterProcessor {

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		if (StringUtils.isBlank(name)){
			throw new SystemException("使用格式不正确,[PAGE:页参数名]");
		}
		
		Integer pageNo = null;
		int pageSize = 20;
		if (parameter != null){
			pageNo = Page.getStartPageNo(parameter);
			pageSize = Page.getPageSize(parameter);
		}
		if (pageNo == null){
			pageNo = 1;
		}
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		if ("start".equals(name)){
			sbSQL.append(page.getStartPosition());
		}
		else if ("pageNo".equalsIgnoreCase(name)){
			sbSQL.append(page.getPageNo());
		}
		else if ("pageSize".equalsIgnoreCase(name) || "limit".equalsIgnoreCase(name)){
			sbSQL.append(page.getPageSize());
		}
		else if ("end".equalsIgnoreCase(name)){
			sbSQL.append(page.getEndPosition());
		}
		else if ("maxnextcount".equalsIgnoreCase(name)){
			sbSQL.append(page.getMaxNextCount());
		}
		else{
			throw new SystemException("[PAGE:的参数名只能是start,end,pageno,pagesize,maxnextcount");
		}
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"PAGE"});
	}

	/*
	@Override
	public void handle(StringBuffer sbSQL, SqlParameter sqlParameter,
			RequestParameter requestParameter,
			List<SqlParameterHandler> handlerList, List<String> paramValue,
			Dialect dialect) {
		String name = sqlParameter.getName();
		if (StringUtils.isBlank(name)){
			throw new SystemException("使用格式不正确,[PAGE:页参数名]");
		}
		
		RequestParameter parameter = DatasetHelper.getCurrentRequestParameter();
		Integer pageNo = null;
		int pageSize = 20;
		if (parameter != null){
			pageNo = parameter.getStartPageNo();
			pageSize = parameter.getPageSize();
		}
		if (pageNo == null){
			pageNo = 1;
		}
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		if ("start".equals(name)){
			sbSQL.append(page.getStartPosition());
		}
		else if ("pageNo".equalsIgnoreCase(name)){
			sbSQL.append(page.getPageNo());
		}
		else if ("pageSize".equalsIgnoreCase(name) || "limit".equalsIgnoreCase(name)){
			sbSQL.append(page.getPageSize());
		}
		else if ("end".equalsIgnoreCase(name)){
			sbSQL.append(page.getEndPosition());
		}
		else if ("maxnextcount".equalsIgnoreCase(name)){
			sbSQL.append(page.getMaxNextCount());
		}
		else{
			throw new SystemException("[PAGE:的参数名只能是start,end,pageno,pagesize,maxnextcount");
		}
	}

	@Override
	public void setParameter(Connection connection,
			PreparedStatement ps, int index, String paramValue)
			throws SQLException {

	}

	@Override
	public SqlParameterTypeEnum getSqlParameterType() {
		return SqlParameterTypeEnum.PAGE;
	}
	*/
}
